package com.liujiaqi;

import io.minio.*;
import io.minio.errors.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class TestMinio {
  public static void main(String[] args) {

    String endpoint = "http://192.168.183.101:9000";
    String accessKey = "minio";
    String secretKey = "minio123";
    String bucketName = "hello-minio";

    MinioClient minioClient = MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();

    try {
      boolean bucketExists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
      if (!bucketExists) {
        minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());

        String policy = """
          {
            "Statement" : [ {
              "Action" : "s3:GetObject",
              "Effect" : "Allow",
              "Principal" : "*",
              "Resource" : "arn:aws:s3:::%s/*"
            } ],
            "Version" : "2012-10-17"
          }
          """.formatted(bucketName);
        minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName).config(policy).build());
      }

      minioClient.uploadObject(UploadObjectArgs.builder().filename("C:\\Users\\Administrator\\Downloads\\radio_disabled.png").bucket(bucketName).object("radio_disabled.png").build());
      System.out.println("上传成功");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
